<template>
<div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="page-tools">
          <!-- 左侧 -->
          <div class="left" style="margin-left: 20px;">
            <div class="tips">
              <span class="typeface">关键字</span>
                <input
                type="text"
                placeholder="根据编号搜索"
                class="input"
                v-model="data.keyword"
                >
            </div>
          </div>
          <div class="right">
            <!-- 右侧 -->
            <el-button size="small" @click="clear">清除</el-button>
            <el-button type="primary" size="small" @click="asd">搜索</el-button>
          </div>
        </div>
        <div class="el-alert" style="margin-bottom: 15px;">
          <i class="el-icon-info" style="margin-right:10px;" />
          <span style="font-size:14px;">数据一共{{ total }}条</span>
        </div>
        <!-- 表格 -->
      <el-card>
        <el-table
          :data="list"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="编号"
            width="200">
          </el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
            width="100">
            <template #default="{row}">
              {{ TypeEnum[row.questionType] }}
            </template>
          </el-table-column>
          <el-table-column
            prop="questionIDs"
            label="题目编号"
            width="200">
            <template #default="{row}">
              <a href="#"
                style="color: rgb(0, 153, 255);"
                v-for="(item, index) in row.questionIDs" :key="index"
              >{{ item.number }}</a>
            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            label="录入时间"
            width="200">
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)">
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率(%)">
          </el-table-column>
          <el-table-column
            prop="userName"
            label="录入人">
          </el-table-column>
          <el-table-column
            width="80px;"
            label="操作"
            >
            <template #default="{row}">
              <el-button
              type="danger"
              icon="el-icon-delete"
              title="删除"
              circle size="small"
              @click="deleteRandoms(row)"
              >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-row type="flex" justify="end" align="middle" style="height:80px;">
          <el-pagination
          @current-change="handleCurrentChange"
          background
          :current-page="data.page"
          :page-sizes="[20, 30, 50, 100]"
          :page-size="data.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>
        </el-row>
      </el-card>
      </el-card>
    </div>
  </div>
</template>
<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'

export default {
  data () {
    return {
      list: [],
      data: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      total: 0,
      // 定义题型枚举
      TypeEnum: {
        1: '单选',
        2: '多选',
        3: '简答'
      }
    }
  },
  async created () {
    this.randoms()
  },
  methods: {
    // 获取列表数据
    async randoms () {
      const res = await randoms(this.data)
      console.log(res)
      this.list = res.data.items
      this.total = res.data.counts
    },
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.data.page = newPage
      this.randoms()
    },
    // 删除员工
    deleteRandoms (data) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发请求
        await removeRandoms(data)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.data.page > 1) {
          this.data.page--
        }
        // 刷新列表
        this.randoms()
      })
    },
    // 清除
    clear () {
      this.data.keyword = ''
    },
    // 查询
    asd () {
      // console.log('ok')
      this.data.page = 1
      this.randoms()
    }
  }
}
</script>

<style lang="scss" scoped>
.page-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;
  ::v-deep.tips {
    line-height: 34px;
    padding: 0px 15px;
    border-radius: 5px;
    border: 1px solid rgba(145, 213, 255, 1);
    background: rgba(230, 247, 255, 1);
    i {
      margin-right: 10px;
      color: #409eff;
    }
  }
}
.typeface {
    text-align: right;
    vertical-align: middle;
    font-size: 14px;
    font-weight: 600;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 5px 5px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.input {
  -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: 12px;
    height: 30px;
    line-height: 30px;
    outline: 0;
    padding: 0 15px;
}
.el-alert {
    width: 100%;
    background-color: #f4f4f5;
    color: #909399;
    margin: 10px;
}
// .button {
//   border-color: #fbc4c4;
//   color: #f56c6c;
//   background: #fef0f0;
// }
</style>
